package com.haochang.chunkvod.app.utils;

import android.text.TextUtils;

/**
 *@author qintao
 *created at 2016/7/13 11:14
 * 价格转换
 */

public class SaleUtils
{
	/**
	 * 分隔符
	 */
	private final static String SPLIT = "\\.";

	/**
	 * 分转换为元
	 * @param price
	 * @return
	 */
	public static String getSaleString(int price)
	{
		if (price == 0)
		{
			return "0";
		} else
		{
			price = price % 100 > 0 ? price / 100 * 100 + 100 : price;//总金额向上取整
			//return price / 100 + "." + price % 100 / 10 + "" + price % 100 % 10;//带有角和分
			return price / 100 + "";//不带有角和分
		}
	}

	/**
	 * 元转换为分
	 * @param price
	 * @return
	 */
	public static int getSaleInt(String price)
	{
		if (!TextUtils.isEmpty(price))
		{
			int yuan = 0, fen = 0;
			String value[] = price.split(SPLIT);
			if (value != null && value.length > 0)
			{
				if (!TextUtils.isEmpty(value[0]))
				{
					yuan = Integer.parseInt(value[0]) * 100;
				}
				if (value.length > 1)
				{
					if (!TextUtils.isEmpty(value[1]))
					{
						if (value[1].length() > 2)
							value[1] = value[1].substring(0, 2);
						else if (value[1].length() < 2)
							value[1] = value[1] + "0";
						fen = Integer.parseInt(value[1]);
					}
				}
				return (yuan + fen);
			} else
				return 0;
		} else
			return 0;
	}
}
